import { Engine } from "@babylonjs/core/Engines/engine";
import { Stage } from "../../base/stage";
import { UIContainer } from './ui-container';


/**
 * ui根容器
 * @param engine 引擎
 * @param stages 舞台集合
 */
class UIRoot {

    private _engine: Engine;
    private _resizeHandle: { (): void };
    private _renderSize = { x: 0, y: 0 };

    /**
     * ui容器集合
     */
    protected containers: UIContainer[];

    constructor(engine: Engine, stages: Stage[]) {
        this._engine = engine;
        this.containers = this.initializeUIContainers(engine, stages);
        this.setSizeRatio(engine);

        this._resizeHandle = () => {
            const rw = engine.getRenderWidth();
            const rh = engine.getRenderHeight();
            if (rw === this._renderSize.x && rh === this._renderSize.y) return;
            this._renderSize.x = rw;
            this._renderSize.y = rh;
            this.resize();
        };
        this._engine.onResizeObservable.add(this._resizeHandle);
    }

    /**
     * 窗口尺寸改变时的响应函数
     */
    public resize(): void {
        this.containers.forEach(c => c.resize());
    }

    /**
     * 初始化ui容器
     * @param engine 引擎 
     * @param stages 舞台集合
     * @returns ui容器集合
     */
    protected initializeUIContainers(engine: Engine, stages: Stage[]): UIContainer[] {
        return [];
    }

    /**
     * 设置尺寸比
     * @param engine 引擎
     */
    private setSizeRatio(engine: Engine): void {
        const ratio = 1 / engine.getHardwareScalingLevel();
        this.containers.forEach(c => c.sizeRatio = ratio);
    }

}


export { UIRoot };